﻿using Comm100.Framework.Domain.Repository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Bot.Domain.Entities;
using Bot.Domain.Interfaces;
using Bot.Domain.Bo;
using Castle.Windsor;
using Bot.Domain.Enum;

namespace Bot.Domain.Categories.Service
{
    public class SubscriptionDomainService : ISubscriptionDomainService
    {
        private IWindsorContainer _container;
        private readonly IRepository<int, Subscription> _repository;

        public SubscriptionDomainService(IRepository<int, Subscription> repository, IWindsorContainer container)
        {
            this._repository = repository;
            this._container = container;
        }

        public Subscription Create(Subscription category)
        {
            return _repository.Create(category);
        }

        public Subscription Get()
        {
            Subscription result = _repository.ListAll().FirstOrDefault();
            if (result != null)
            {
                if (result.NextRenewDate < DateTime.UtcNow)
                {
                    //判断有没有过期
                    var startDate = result.StartDate;
                    var endDate = result.NextRenewDate;
                    if (result.PaymentPeriod == EnumPaymentPeriod.Monthly)
                    {
                        endDate = result.StartDate.AddMonths(1);
                    }
                    else if (result.PaymentPeriod == EnumPaymentPeriod.Quarterly)
                    {
                        endDate = result.StartDate.AddMonths(3);
                    }
                    else if (result.PaymentPeriod == EnumPaymentPeriod.Yearly)
                    {
                        endDate = result.StartDate.AddMonths(12);
                    }
                    if (DateTime.UtcNow >= startDate && DateTime.UtcNow < endDate)
                    {
                        //没有过期, renew
                        var renewDate = new DateTime(DateTime.UtcNow.Year, DateTime.UtcNow.Month, result.StartDate.Day);
                        if (renewDate < DateTime.UtcNow)
                        {
                            renewDate = renewDate.AddMonths(1);
                        }
                        if (renewDate > endDate)
                        {
                            renewDate = endDate;
                        }
                        SubscriptionUpdateBo bo = new SubscriptionUpdateBo()
                        {
                            SiteId = result.SiteId,
                            StartDate = result.StartDate,
                            NextRenewDate = renewDate,
                            TotalPurchasedMessageAmount = result.MesssageQuotaPerMonth,
                            MesssageQuotaPerMonth = result.MesssageQuotaPerMonth,
                            IfSentedQuotaAlmostUsedOutMail = false,
                            MonthlyListPrice = result.MonthlyListPrice,
                            OverageMessageQuota = 0,
                            OverageUnitPrice = 0,
                            PaymentPeriod = result.PaymentPeriod,
                            QuarterlyListPrice = result.QuarterlyListPrice,
                            YearlyListPrice = result.YearlyListPrice
                        };
                        Update(bo);
                        result.NextRenewDate = renewDate;
                        result.IfSentedQuotaAlmostUsedOutMail = false;
                        result.OverageMessageQuota = 0; 
                        result.OverageUnitPrice = 0;
                    }
                }
            }
            return result;
        }

        public Subscription Update(SubscriptionUpdateBo bo)
        {
            Subscription subscription = _repository.Get(bo.SiteId);
            subscription.SiteId = bo.SiteId;
            subscription.StartDate = bo.StartDate;
            subscription.NextRenewDate = bo.NextRenewDate;
            subscription.TotalPurchasedMessageAmount = bo.TotalPurchasedMessageAmount;
            subscription.PaymentPeriod = bo.PaymentPeriod;
            subscription.MesssageQuotaPerMonth = bo.MesssageQuotaPerMonth;
            subscription.MonthlyListPrice = bo.MonthlyListPrice;
            subscription.QuarterlyListPrice = bo.QuarterlyListPrice;
            subscription.YearlyListPrice = bo.YearlyListPrice;
            subscription.OverageMessageQuota = bo.OverageMessageQuota;
            subscription.OverageUnitPrice = bo.OverageUnitPrice;
            subscription.IfSentedQuotaAlmostUsedOutMail = bo.IfSentedQuotaAlmostUsedOutMail;

            _repository.Update(subscription);

            return subscription;

        }
    }
}
